package com.rx.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rx.entity.Admin;
import com.rx.entity.Logs;
import com.rx.entity.Role;
import com.rx.service.AdminServiceI;
import com.rx.service.AdminServiceImpl;
import com.rx.service.LogsServiceI;
import com.rx.service.LogsServiceImpl;
import com.rx.service.RoleServiceI;
import com.rx.service.RoleServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/admin/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 管理员
		AdminServiceI asi = new AdminServiceImpl();
		// 角色
		RoleServiceI rsi = new RoleServiceImpl();

		String flag = request.getParameter("flag");
		// 退出登录
		if ("out".equals(flag)) {
			// 销毁session
			HttpSession session = request.getSession();
			System.out.println("退出登录");
			session.invalidate();
			response.sendRedirect("/admin/login.html");
		} else if ("add".equals(flag)) {
			List<Role> roleList;
			try {
				// 读取角色
				roleList = rsi.getAll();
				request.setAttribute("roleList", roleList);
				// 请求转发
				request.getRequestDispatcher("/admin/addAdmin.jsp").forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			// 读数据使用get
			try {
				int curPage = 1;
				int pageSize = 3;
				Map<String,Object> adminMap = asi.getAdminAndRolePageAll(curPage, pageSize);
				// 通过 request 将获取到的数据传给页面
				request.setAttribute("map", adminMap);
				// 请求转发
				request.getRequestDispatcher("/admin/admin.jsp").forward(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * view界面跳转到该方 此方法会调用 业务层的方法进行用户名密码认证
	 * 
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		// 创建业务层对象
		AdminServiceI asi = new AdminServiceImpl();

		String flag = request.getParameter("flag");
		if ("login".equals(flag)) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			try {
				// 判断用户密码是否正确
				if (asi.login(username, password)) {
					// 获取一个会话，将用户值放入会话中
					HttpSession session = request.getSession();
					//
					session.setAttribute("username", username);
					/***************** 添加登录日志 *******************/
					LogsServiceI lsi = new LogsServiceImpl();
					Logs logs = new Logs();
					logs.setOperateType(0);
					logs.setContent(username + "登录成功。");
					lsi.add(logs);

					// response.sendRedirect("/admin/index.jsp");
					response.getWriter().write("success");
				} else {
					// response.sendRedirect("/error.jsp");
					response.getWriter().write("fail");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("add".equals(flag)) {
			String txtAccount= request.getParameter("txtAccount");
			String adminName= request.getParameter("txtUserName");
			String gender=request.getParameter("txtGender");
			String mobile = request.getParameter("txtMobile");
			String email = request.getParameter("txtEmail");
			String password = request.getParameter("txtPassword");
			String roleId = request.getParameter("roleType");
			String deptId=request.getParameter("deptId");
			//创建Admin对象
			Admin admin=new Admin();
			admin.setAdmin(txtAccount);
			admin.setAdminName(adminName);
			admin.setGender(Integer.valueOf(gender));
			admin.setMobile(mobile);
			admin.setEmail(email);
			admin.setPassword(password);
			admin.setRoleId(Integer.valueOf(roleId));
			admin.setDeptId(Integer.valueOf(deptId));
			try {
				if(asi.add(admin)) {
					response.sendRedirect("/admin/AdminServlet");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}else if("exist".equals(flag)) {
			String account= request.getParameter("txtAccount");
			//查找账号
			try {
				if(asi.getAdminExsit(account)) {
					response.getWriter().write("1");
				}else {
					response.getWriter().write("0");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
